﻿namespace WMS6.Web.Billing
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BillingServiceRef;

    public class Statement : BasePage
    {
        protected Button btnConfirm;
        protected Button btnGoBack;
        protected Button btnPay;
        protected Button btnRun;
        protected CheckBox chkAccessorial;
        protected CheckBox chkInBound;
        protected CheckBox chkInventory;
        protected CheckBox chkOutBound;
        protected DropDownList ddlStatus;
        protected HtmlForm F9201;
        protected GridView gvStatementDetail;
        protected HtmlInputHidden hdncalendarFlag;
        protected HtmlInputHidden hdnDataFormat;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnLineID;
        protected HtmlInputHidden hdnStatementID;
        protected HtmlHead Head1;
        protected HtmlImage imgOwnerID;
        protected Label Label1;
        protected Label lblEndTime;
        protected Label lblMsg;
        protected Label lblOwner;
        protected Label lblPaymentDate;
        protected Label lblRemark;
        protected Label lblStatementDate;
        protected Label lblStatementID;
        protected Label lblStatus;
        protected Label lblTotalReceivable;
        protected Label lblTotalReceived;
        protected HtmlLink mycss;
        protected Label one1;
        protected Label one2;
        protected ScriptManager ScriptManager1;
        protected TextBox txtEndTime;
        protected TextBox txtOwnerID;
        protected TextBox txtPaymentDate;
        protected TextBox txtRemark;
        protected TextBox txtStatementDate;
        protected TextBox txtStatementID;
        protected TextBox txtTotalReceivable;
        protected TextBox txtTotalReceived;
        protected UpdatePanel up1;

        protected void btnConfirm_Click(object sender, EventArgs e)
        {
            this.UpdateStatementInfo("Confirm");
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("StatementQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("StatementList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnPay_Click(object sender, EventArgs e)
        {
            this.UpdateStatementInfo("Payment");
        }

        protected void btnRun_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtOwnerID.Text))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9200_003");
            }
            else if (string.IsNullOrEmpty(this.txtEndTime.Text))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9200_004");
            }
            else
            {
                this.InsertStatementInfo();
            }
        }

        protected void gvStatementDetail_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F9201", this.gvStatementDetail);
        }

        protected void gvStatementDetail_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            (sender as GridView).PageIndex = e.NewPageIndex;
            (sender as GridView).DataSource = (base.DataEntityViewState as StatementInfo).StatementDetailInfos;
            (sender as GridView).DataBind();
            ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,2) </script>", false);
        }

        protected void gvStatementDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int i = this.gvStatementDetail.PageIndex * this.gvStatementDetail.PageSize;
                e.Row.Cells[0].Text = ((e.Row.RowIndex + 1) + i).ToString();
                base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblRemark"));
            }
        }

        protected void gvStatementDetail_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                string sortDirection = string.Empty;
                if (base.GridViewSortDirection == SortDirection.Ascending)
                {
                    base.GridViewSortDirection = SortDirection.Descending;
                    sortDirection = "DESC";
                }
                else
                {
                    base.GridViewSortDirection = SortDirection.Ascending;
                    sortDirection = "ASC";
                }
                StatementInfo statementInfo = base.DataEntityViewState as StatementInfo;
                DataView dv = CADataConverter.ToDataTable(statementInfo.StatementDetailInfos).DefaultView;
                dv.Sort = e.SortExpression + " " + sortDirection;
                base.BindData2GridView(this.gvStatementDetail, dv);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,2);</script>", false);
            }
        }

        private void InsertStatementInfo()
        {
            try
            {
                StatementInfo StatementInfo = new StatementInfo {
                    StatementID = this.txtStatementID.Text,
                    OwnerID = this.txtOwnerID.Text,
                    StartTime = DateTime.Now,
                    Status = "New",
                    EndTime = Convert.ToDateTime(this.txtEndTime.Text),
                    Remark = this.txtRemark.Text,
                    InBound = this.chkInBound.Checked ? "Y" : "N",
                    OutBound = this.chkOutBound.Checked ? "Y" : "N",
                    Inventory = this.chkInventory.Checked ? "Y" : "N",
                    Accessorial = this.chkAccessorial.Checked ? "Y" : "N",
                    StatementDate = DateTime.Now,
                    PaymentDate = DateTime.Now,
                    CreatedBy = base.UserInfo.UserID
                };
                this.hdnStatementID.Value = BillingServiceClientProxy.GetInstance().InsertStatement(StatementInfo);
                this.hdnLineID.Value = "10";
                this.hdnFlag.Value = "1";
                base.DataEntityViewState = StatementInfo;
                this.txtStatementID.Text = this.hdnStatementID.Value;
                this.txtStatementID.Enabled = false;
                this.SetStatementInfo2Form();
                this.lblMsg.Text = this.hdnStatementID.Value + base.GetGlobalMsgResource("F9200_007");
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9200_008") + ex.Message;
            }
        }

        private void LoadData()
        {
            base.BindData2DDLBySysCode(this.ddlStatus, "StatementStatu");
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnFlag.Value = "1";
                this.hdnStatementID.Value = base.Request.QueryString["ID"].ToString();
                this.SetStatementInfo2Form();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["detail"]))
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", " <script type=\"text/javascript\" >setTab('one',2,2) </script>", false);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetStatementInfo2Form()
        {
            try
            {
                StatementInfo StatementInfo = BillingServiceClientProxy.GetInstance().GetStatementByID(this.hdnStatementID.Value);
                if (StatementInfo == null)
                {
                    return;
                }
                this.txtStatementID.Text = StatementInfo.StatementID;
                this.txtStatementID.Enabled = false;
                this.txtOwnerID.Text = StatementInfo.OwnerID;
                this.txtEndTime.Text = base.GetPreferenceShortDate(StatementInfo.EndTime);
                this.txtRemark.Text = StatementInfo.Remark;
                string CS40001 = StatementInfo.Status;
                if (CS40001 != null)
                {
                    if (!(CS40001 == "Confirm"))
                    {
                        if (CS40001 == "Payment")
                        {
                            goto Label_00C5;
                        }
                    }
                    else
                    {
                        this.txtStatementDate.Text = base.GetPreferenceShortDate(StatementInfo.StatementDate);
                    }
                }
                goto Label_00F7;
            Label_00C5:
                this.txtPaymentDate.Text = base.GetPreferenceShortDate(StatementInfo.PaymentDate);
                this.txtStatementDate.Text = base.GetPreferenceShortDate(StatementInfo.StatementDate);
            Label_00F7:
                this.txtTotalReceivable.Text = "0";
                this.txtTotalReceived.Text = "0";
                this.ddlStatus.SelectedValue = StatementInfo.Status;
                if ((StatementInfo.StatementDetailInfos != null) && (StatementInfo.StatementDetailInfos.Count > 0))
                {
                    this.hdnLineID.Value = (StatementInfo.StatementDetailInfos[StatementInfo.StatementDetailInfos.Count - 1].LineID + 10).ToString();
                    decimal dReceivable = 0M;
                    decimal dReceived = 0M;
                    foreach (StatementDetailInfo info in StatementInfo.StatementDetailInfos)
                    {
                        dReceivable += info.Receivable;
                        dReceived += info.Received;
                    }
                    this.txtTotalReceivable.Text = base.GetPreferenceDigit(dReceivable);
                    this.txtTotalReceived.Text = base.GetPreferenceDigit(dReceived);
                }
                else
                {
                    this.hdnLineID.Value = "10";
                }
                base.BindData2GridView(this.gvStatementDetail, StatementInfo.StatementDetailInfos);
                base.DataEntityViewState = StatementInfo;
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void UpdateStatementInfo(string Status)
        {
            try
            {
                StatementInfo StatementInfo = base.DataEntityViewState as StatementInfo;
                string CS40000 = Status;
                if (CS40000 != null)
                {
                    if (!(CS40000 == "Confirm"))
                    {
                        if (CS40000 == "Payment")
                        {
                            goto Label_003D;
                        }
                    }
                    else
                    {
                        StatementInfo.StatementDate = DateTime.Now;
                    }
                }
                goto Label_004B;
            Label_003D:
                StatementInfo.PaymentDate = DateTime.Now;
            Label_004B:
                StatementInfo.Status = Status;
                StatementInfo.UpdatedDate = DateTime.Now;
                StatementInfo.Remark = this.txtRemark.Text;
                this.lblMsg.Text = BillingServiceClientProxy.GetInstance().UpdateStatement(StatementInfo) + base.GetGlobalMsgResource("F9200_005");
                this.SetStatementInfo2Form();
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9200_006") + ex.Message;
            }
        }
    }
}

